﻿import QtQuick 2.0
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0
import QtQuick.Layouts 1.12
import "../components"

Window {

    id: root


    BoxError{
        id: boxError_repeat
        box_text: "设备已添加"
    }

    title: openType===0 ? "添加设备" : "查看设备"

    function showNewDevice(){
        openType = 0
        connType = 0
        edit_name.setText("")
        edit_route.setText("")
        edit_port.setText("")
        editable = true
        root.show()
    }
    function showExistDevice(equId){
        openType = 1
        root.equId = equId
        var equInfo = CSetUp.querySingleEquInfo(equId)
        connType = equInfo["EquConnectType"].toString()==="0" ? 0 : 1
        edit_name. setText(equInfo["EquName"])
        edit_route.setText(equInfo["EquConnectType"].toString()==="0" ? equInfo["Ip"] : equInfo["SerialPort"])
        edit_port. setText(equInfo["EquConnectType"].toString()==="0" ? equInfo["Port"] : equInfo["BaudRate"])
        if(equInfo["EquStatus"].toString()==="2"){
            editable = false
        }else{
            editable = true
        }
        root.show()
    }
    modality: Qt.WindowModal


    function addDevice(){

        if(edit_route.getText() === "" || edit_port.getText() === "" || edit_name.getText() === ""){
            mainwindow.showToast("请完整填写设备信息", 2)
            return
        }

        var vmap = ({});
        //        vmap["Id"] = openType===0 ? rootSettingsDevice.gEquList.length : root.equId
        if(openType===1){
            vmap["Id"] = root.equId
        }
        vmap["EquName"] = edit_name.getText()
        vmap["EquConnectType"] = connType
        vmap["Ip"] = edit_route.getText()
        vmap["Port"] = edit_port.getText()
        vmap["SerialPort"] = edit_route.getText()
        vmap["BaudRate"] = edit_port.getText()
        let res = CSetUp.addNewEqu(vmap)
        if(res === 1){
            mainwindow.showToast("连接成功", 0)
            root.close()
        }else if(res === 2){
            //            mainwindow.showToast("设备地址已存在", 1)
            boxError_repeat.show()
        }else{
            mainwindow.showToast("连接失败", 1)
            root.close()
        }
        rootSettingsDevice.refresh()
    }

    property int connType: 0    // 0:IP   1:串口
    property string name: ""
    property string route: ""
    property string port: ""
    property int openType: 0    // 0:新建  1:查看
    property bool editable: true        // 查看时 设备是否可以修改
    property string equId: ""


    minimumHeight: 360*dp
    maximumHeight: 360*dp
    minimumWidth: 480*dp
    maximumWidth: 480*dp

    Column{           // 网络连接
        height: root.height - 140*dp
        width: root.width - 80*dp
        x: 40*dp; y: 40*dp
        spacing: 12*dp
        LineEditor{
            id: edit_name
            title: "设备名称"
            anchors.horizontalCenter: parent.horizontalCenter
            lengthVisible: true
            required: true
            editWidth: 260*dp
            inputMaxLength: 30
            editable: root.editable
            Keys.onTabPressed: {
                focus = false
                edit_route.focus = true
            }
            onEnterPressed: {
                button_accept.released()
            }
        }
        Item{
            height: 40*dp
            width: 340*dp
            anchors.horizontalCenter: parent.horizontalCenter

            LineEditor{
                title: "通信类型"
                required: true
                editWidth: 0
                editable: root.editable
            }

            CustomRadio{
                x: 80*dp
                title: "网络通信"
                isChecked: connType === 0
                onReleased: connType = 0
                editable: root.editable
            }
            CustomRadio{
                x: 240*dp
                title: "USB通信"
                isChecked: connType === 1
                onReleased: connType = 1
                editable: root.editable
            }
        }

        LineEditor{
            id: edit_route
            title: connType===0 ? "IP  地  址" : "串  口  号"
            anchors.horizontalCenter: parent.horizontalCenter
            required: true
            editWidth: 260*dp
            ipflag: connType===0
            inputMaxLength: 15
            editable: root.editable
            Keys.onTabPressed: {
                focus = false
                edit_port.focus = true
            }
            onEnterPressed: {
                button_accept.released()
            }
        }LineEditor{
            id: edit_port
            title: connType===0 ? "端  口  号" : "波  特  率"
            anchors.horizontalCenter: parent.horizontalCenter
            required: true
            editWidth: 260*dp
            intflag: true
            inputMaxLength: 10
            editable: root.editable
            Keys.onTabPressed: {
                focus = false
                edit_name.focus = true
            }
            onEnterPressed: {
                button_accept.released()
            }
        }Item{
            height: 16*dp
            width: parent.width
            Text{
                color: "red"
                text: "当前设备运行中，无法修改设备信息"
                font.pixelSize: config.fontsize-1
                anchors.centerIn: parent
            }
            anchors.horizontalCenter: parent.horizontalCenter
            visible: !editable
        }
    }


    Item{                       // 确定 / 取消
        anchors.horizontalCenter: parent.horizontalCenter
        height: 42*dp
        width: 320*dp
        y: root.height - 90*dp
        PushButton{
            id: button_reject
            x: 30*dp
            title: "取消"
            defaultMode2: true
            onReleased: {
                root.close()
            }
        }PushButton{
            id: button_accept
            x: 170*dp
            title: root.editable ? "连接" : "确定"
            defaultMode1: true
            onReleased: {
                if(!editable){
                    root.close()
                    return
                }
                addDevice()
            }
        }








    }
}
